package com.adcc.loadsheet.movingdata.controller;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.movingdata.service.LoadsheetService;
import com.adcc.loadsheet.movingdata.vo.ResultStatus;
import com.adcc.loadsheet.movingdata.vo.ResultVO;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import org.apache.ibatis.ognl.IntHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by AN on 2020-05-12.
 */
@RestController
@RequestMapping(value = "/move", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "数据移动接口", description = "数据移动接口",position = 0)
public class MainController {

    @Autowired
    private LoadsheetService loadsheetService;

    /**
     * 舱单名单数据移动接口
     * @return
     */
    @RequestMapping(value = "/moveLSandPS/{startTime}/{endTime}", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO moveLSandPS(@PathVariable("startTime")String startTime, @PathVariable("endTime")String endTime) {
        ResultVO vo = new ResultVO();
        //校验传参是否合法
        if (Strings.isNullOrEmpty(startTime)) {
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_PARAMS_INVALID, "startTime"));
            return vo;
        }

        if (Strings.isNullOrEmpty(endTime)) {
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_PARAMS_INVALID, "endTime"));
            return vo;
        }
        try {
            Map<String, Object> data= Maps.newHashMap();
            String tips="共同步"+loadsheetService.moveLoadsheetDataBetween(startTime,endTime)+"条数据！";
            data.put("Tips",tips);
            vo.setData(data);
        }catch (Exception ex){
            Log.error(MainController.class,"请求及时更新舱单名单数据出错",ex);
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_CALL_FAILED, "moving loadsheet and passgerlist data"));
            loadsheetService.insertFaildate(startTime,endTime);
        }
        return vo;
    }

    /**
     * 舱单名单数据移动接口
     * @return
     */
    @RequestMapping(value = "/moveBase", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO moveBase() {
        ResultVO vo = new ResultVO();
        try {
            loadsheetService.moveStaff();
            loadsheetService.moveDepartment();
            loadsheetService.moveAirlines();
            loadsheetService.moveAirports();
            loadsheetService.updatebasetime();
        }catch (Exception ex){
            Log.error(MainController.class,"请求及时更新基础数据出错",ex);
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_CALL_FAILED, "moving base data"));
        }
        return vo;
    }
}
